home *** CD-ROM | disk | FTP | other *** search
/ BMUG Revelations / BMUG Revelations.toast / Programming / Programming Utilities / MPW Extras / Scripts / RestoreFiles < prev    next >
Encoding:
Text File  |  1991-10-05  |  8.6 KB  |  259 lines  |  [TEXT/MPS ]

  1. #    RestoreFiles - use a Remember-generated backup file and compare it to the current version
  2. #
  3. #    Usage:
  4. #        RestoreFiles [-9 | -13 | -19 | -b x y | -Portrait | -TwoPage]
  5. #            -9            use 9 inch screen (512 x 342)
  6. #            -13            use 13 inch screen (640 x 480)
  7. #            -19            use 19 inch screen (1024 x 768)
  8. #            -Portrait    use 15 inch screen (640 x 870)
  9. #            -TwoPage    use 21 inch screen (1152 x 870)
  10. #            -b x y    use screen dimensions given in x y
  11. #            default is -13
  12. #        NOTE:  
  13. #            Order of parameters is important
  14. #            No error checking is done on x y coordinates
  15. #
  16. #    Returns:
  17. #        Status = 0 - files match
  18. #        Status = 1 - syntax error
  19. #        Status = 2 - files differ  (this is the same status as the compare tool)
  20. #
  21. #    RestoreFiles is a modified version of CompareFiles,
  22. #    which compares two files, and if they don't match it will 
  23. #    bring them both up on the screen and put them side by side.  A menu will be 
  24. #    appended to the menu bar to go through the changes.  When all the changes 
  25. #    have been shown, the file windows will be restored and closed.
  26. #
  27. #    The differences are shown from bottom to top so that if any changes are made
  28. #    in the files, the line offsets are not messed up.
  29. #
  30. #    The script creates two temporary files which are deleted on exit.
  31. #
  32. #    Understanding the code…
  33. #
  34. #        RestoreFiles is actually a few scripts rolled into one.  The action
  35. #        depends on the parameters.  Here is what happens:
  36. #
  37. #        RestoreFiles
  38. #            Gets the files to be compared, and generates the difference script
  39. #            If there are differences, it will
  40. #            create a script to execute on quit
  41. #            move the windows around next to each other
  42. #            create the Restore Menu
  43. #            call itself with -n
  44. #        RestoreFiles -n                            - show next change
  45. #        RestoreFiles -New                        - cut change from new to scrap
  46. #        RestoreFiles -old                        - paste change from new into old
  47. #        RestoreFiles -q                            - execute Quit script
  48. #
  49. #    Portions copyright Apple Computer, Inc. 1988 - 1990
  50. #    All rights reserved.
  51.  
  52. Set ScriptName "{0}"                                                        # save the script's name
  53. Set Exit 0
  54. Set ErrorMessage     "### {0} - Incorrect order, or missing or illegal option."
  55. Set Usage             "# Usage - {0} [-9 | -13 | -19 | -b x y | -Portrait | -TwoPage]."
  56.  
  57. If "{1}" == "-n"                                                            # find next change                
  58.     Find ¡1    "{ShellDirectory}"Restore_File                                    # get past current Line
  59.     Find \###\ "{ShellDirectory}"Restore_File
  60.     If {Status} != 0                                                        # end of change log
  61.         Beep
  62.         Confirm "No more changes.  Are you done comparing these Files?"
  63.         If {Status} == 0
  64.             DeleteMenu Restore
  65.             Close -n "{ShellDirectory}Restore_File" ≥ Dev:Null
  66.             "{ShellDirectory}Restore_State" ≥ Dev:Null
  67.             Delete "{ShellDirectory}Restore_File" ≥ Dev:Null
  68.             Delete "{ShellDirectory}Restore_State" ≥ Dev:Null
  69.         Set RememberActive 1                                                # re-enable Remember
  70.         End  # If {Status}
  71.     Else
  72.         Find !1 "{ShellDirectory}"Restore_File                                # select entire Line
  73.         Mark -y § Current_Line "{ShellDirectory}"Restore_File
  74.         Execute "{ShellDirectory}Restore_File.§"
  75.     End  # If {Status}
  76. Else If "{1}" == "-q"                                                        # Quit
  77.     DeleteMenu Restore
  78.     Close -n "{ShellDirectory}Restore_File" ≥ Dev:Null
  79.     "{ShellDirectory}Restore_State" ≥ Dev:Null
  80.     Delete "{ShellDirectory}Restore_File" ≥ Dev:Null
  81.     Delete "{ShellDirectory}Restore_State" ≥ Dev:Null
  82.     Set RememberActive 1                                                    # re-enable Remember
  83. Else If "{1}" == "-New"                                                        # cut selection from new to scrap
  84.     Find Current_Line "{ShellDirectory}"Restore_File ∑ Dev:Null                # re-sync the two files
  85.     Set MyStatus {Status}     
  86.     If {MyStatus} != 0
  87.         Exit {MyStatus}
  88.     End  # If {MyStatus}
  89.     Execute "{ShellDirectory}Compare_File.§" ∑ Dev:Null
  90.     Set selChars `Position -C "{Target}" ≥ Dev:Null`                        # see if we need to copy/paste or just cut
  91.     (Evaluate {selChars} =~ /([0-9]+)®1,([0-9]+)®2/ ) ∑ Dev:Null
  92.     If {®1} == {®2}
  93.         Cut § "{Active}" ∑ Dev:Null
  94.     Else
  95.         Copy § "{Target}" ∑ Dev:Null
  96.         Mark -y § Compare_Mark "{Active}"
  97.         Paste § "{Active}" ∑ Dev:Null
  98.         Find Compare_Mark "{Active}" ∑ Dev:Null
  99.         Unmark Compare_Mark "{Active}" ∑ Dev:Null
  100.     End  # If {®1}
  101. Else If "{1}" == "-old"                                                        # copy selection from old to new
  102.     Find Current_Line "{ShellDirectory}"Restore_File ∑ Dev:Null                # re-sync the two files
  103.     Set MyStatus {Status}     
  104.     If {MyStatus} != 0
  105.         Exit {MyStatus}
  106.     End  # If {MyStatus}
  107.     Execute "{ShellDirectory}Restore_File.§" ∑ Dev:Null
  108.     Set selChars `Position -C "{Active}" ≥ Dev:Null`                        # see if we need to copy/paste or just cut
  109.     (Evaluate {selChars} =~ /([0-9]++)®1,([0-9]++)®2/ ) ∑ Dev:Null
  110.     If {®1} == {®2}
  111.         Cut § "{Target}" ∑ Dev:Null
  112.     Else
  113.         Copy § "{Active}" ∑ Dev:Null
  114.         Mark -y § Restore_Mark "{Target}" ∑ Dev:Null
  115.         Paste § "{Target}" ∑ Dev:Null
  116.         Find Restore_Mark "{Target}" ∑ Dev:Null
  117.         Unmark Restore_Mark "{Target}" ∑ Dev:Null
  118.     End  # If {®1}
  119. Else                                                                        # restore the files and set up menu
  120.     If "{1}" == "-b" || "{1}" == "-B"                                        # user specified size
  121.         If {#} != 5
  122.             Alert "{ErrorMessage}∂n∂n{Usage}"
  123.             Exit 1
  124.         End  # If {#}
  125.         Evaluate Right = {2} - 5
  126.         Evaluate Bottom = {3} - 140
  127.         Set TileRectangle "0,5,{Bottom},{Right}"
  128.         Evaluate Bottom = {Bottom} + 1
  129.         Evaluate Right = {Right} - 9
  130.         Shift 3
  131.     Else If "{1}" == "-9"                                                    # MAC+ and MacSE screen
  132.         If {#} != 1
  133.             Alert "{ErrorMessage}∂n∂n{Usage}"
  134.             Exit 1
  135.         End  # If {#}
  136.         Set TileRectangle "0, 5, 234, 510"
  137.         Set Bottom 235
  138.         Set Right 500
  139.         Shift 1
  140.     Else If "{1}" == "-13"                                                    # MacII screen
  141.         If {#} != 1
  142.             Alert "{ErrorMessage}∂n∂n{Usage}"
  143.             Exit 1
  144.         End  # If {#}
  145.         Set TileRectangle "0, 5, 344, 635"
  146.         Set Bottom 345
  147.         Set Right 626
  148.         Shift 1
  149.     Else If "{1}" == "-19"                                                    # 19" screen (1024 x 768)
  150.         If {#} != 1
  151.             Alert "{ErrorMessage}∂n∂n{Usage}"
  152.             Exit 1
  153.         End  # If {#}
  154.         Set TileRectangle "0, 5, 647, 952"
  155.         Set Bottom 648
  156.         Set Right 945
  157.         Shift 1
  158.     Else If "{1}" =~ /-Portrait/                                            # Apple Macintosh Portrait Display
  159.         If {#} != 1
  160.             Alert "{ErrorMessage}∂n∂n{Usage}"
  161.             Exit 1
  162.         End  # If {#}
  163.         Set TileRectangle "0, 5, 749, 635"
  164.         Set Bottom 750
  165.         Set Right 626
  166.         Shift 1
  167.     Else If "{1}" =~ /-TwoPage/                                                # Apple Two-Page Monochrome Monitor
  168.         If {#} != 1
  169.             Alert "{ErrorMessage}∂n∂n{Usage}"
  170.             Exit 1
  171.         End  # If {#}
  172.         Set TileRectangle "0, 5, 749, 1148"
  173.         Set Bottom 750
  174.         Set Right 1138
  175.         Shift 1
  176.     Else                                                                    # MacII screen (default)
  177.         If {#} != 2
  178.             Alert "{ErrorMessage}∂n∂n{Usage}"
  179.             Exit 1
  180.         End  # If {#}
  181.         Set TileRectangle "0, 5, 344, 635"
  182.         Set Bottom 345
  183.         Set Right 626
  184.     End  # If {1}
  185.     
  186.     # Make a list of the relevant remembered backup files
  187.     Set OldDir "`Directory`"
  188.     Directory "{Backup}"
  189.     Set LeafName "`ParseFileName -l "{Active}"`"                                    # Get the current file's leafname
  190.     Set FileList "`Begin; Files "{LeafName}".[0-9]+; End ≥ Dev:Null`"
  191.     Directory "{OldDir}"
  192.  
  193.     If ("{FileList}" != "")
  194.         Set RemName "`GetListItem -m "Select remembered file:" -q -s {FileList}`"
  195.         If ("{RemName}" != "")    
  196.             Set 1 "`Files -f -q "{Active}" ≥ Dev:Null`"                                # this is the current file
  197.             Set 2 "`Files -f -q "{Backup}{RemName}" ≥ Dev:Null`"                    # this is the remembered Backup file name
  198.             OpenStatus "{1}"
  199.             Set openFile1 {Status}
  200.             OpenStatus "{2}"
  201.             Set openFile2 {Status}
  202.             Compare -m "{1}" "{2}" > "{ShellDirectory}"Restore_File ≥ Dev:Null        # do the comparison
  203.             
  204.             If {openFile1} == 0
  205.                 Open "{1}" ∑ Dev:Null
  206.             End  # If {openFile1}
  207.             If {openFile2} == 0
  208.                 Open -r "{2}" ∑ Dev:Null
  209.             End  # If {openFile2}
  210.             Begin                                                                    # create end script
  211.                 MoveWindow "{1}"                                                    # get the current window states
  212.                 SizeWindow "{1}"
  213.                 MoveWindow "{2}"
  214.                 SizeWindow "{2}"
  215.                 If {openFile1} == 0
  216.                     Echo Close ∂""{1}"∂"
  217.                 End  # If {openFile1}
  218.                 If {openFile2} == 0
  219.                     Echo Close ∂""{2}"∂"
  220.                 End  # If {openFile2}
  221.             End > "{ShellDirectory}"Restore_State
  222.     
  223.             Open "{ShellDirectory}"Restore_File
  224.             Find ∞ "{ShellDirectory}"Restore_File
  225.             TileWindows -v -r "{TileRectangle}" "{1}" "{2}"
  226.             MoveWindow 7 {Bottom} "{ShellDirectory}"Restore_File
  227.             SizeWindow {Right} 75 "{ShellDirectory}"Restore_File
  228.             AddMenu Restore 'Find Next Change' 'Execute Begin; RestoreFiles "-n" ∑ Dev:Null; End'
  229.             AddMenu Restore '(-' ''
  230.             AddMenu Restore 'Copy Selection »»' 'Execute Begin; RestoreFiles "-New" ∑ Dev:Null; End'
  231.             AddMenu Restore '«« Copy Selection' 'Execute Begin; RestoreFiles "-old" ∑ Dev:Null; End'
  232.             AddMenu Restore '(-' ''
  233.             AddMenu    Restore 'Done'    'Execute Begin; RestoreFiles "-q" ∑ Dev:Null; End'
  234.             Set RememberActive 0                                                    # disable Remember
  235.             Execute Begin; RestoreFiles "-n" ∑ Dev:Null; End
  236.         End  # If {RemName}
  237.     Else
  238.         Alert "{ScriptName}:∂nNo remembered files found for {Active}."
  239.     End  # If {FileList}
  240. End  # If {1}
  241.  
  242. Unset ErrorMessage
  243. Unset Usage
  244. Unset LeafName 
  245. Unset FileList
  246. Unset OldDir
  247. Unset openFile1
  248. Unset openFile2
  249. Unset 1
  250. Unset 2
  251. Unset RemName
  252. Unset FileList
  253. Unset selChars
  254. Unset MyStatus
  255. Unset TileRectangle
  256. Unset Bottom
  257. Unset Right
  258.  
  259. # end of RestoreFiles